<?php
/**
 * AJAX Handler for Projects: Check - Return an ajax call that states if the projects alias exists
 * 
 * I have made sure Libdebug is silent in AJAX. This is because it can intefere with the HTML/Javascript as Libdebug Appends after </html>.
 * If you have a problem and need to debug comment $_LIBDEBUG->silence(); (the framework must also be in debugging mode for this to work)
 */

    require_once('../../SiteIncludes.GLOBAL.php');
    
    if(EclipseMDE::runningOnMod_Rewrite() === false) {
	// Security Guard, Do Not Let This be accessed by any other means than mod_rewrite
	$gVAR['e'] = '404';
	require_once('error_handler.php');
    }
    
    if(isset($_LOGGEDINUSER) === true && $_LOGGEDINUSER !== false) {
	/*
	 * Remember if editing: We must enforce some rules for the user
	 * Rule 1:	Must have characters from 0-9a-zA-Z_ (Apache mod_rewrite is enforcing this in the .htaccess for aliases later on.)
	 * Rule 2:	Must have AT LEAST one letter. 
	 *		This is so we do not break the resolution system in $Project
	 */
	if(isset($pVAR['ib_text_nametocheck']) === false) {
	    // They have not sent us a post variable we were expecting
	    $callback = <<<EOERROR
	    parent.EDJXConfirmer.show_nok();	
EOERROR;
	} else {
	    $match = preg_match('/^([a-z0-9_]+)?([a-z]+)([a-z0-9_]+)?$/i', $pVAR['ib_text_nametocheck']);
	    if($match && strlen($pVAR['ib_text_nametocheck']) < 32) {
		/*
		 * This might look back to front! But it's not.
		 * If we catch an exception then does not exist.
		 * That is why the OK block is in the exception
		 */
		try {
		    $ProjectStash->get($pVAR['ib_text_nametocheck']);
		    $callback = <<<EOSUCC
		    parent.EDJXConfirmer.show_nok();
EOSUCC;
		} catch(InvalidProjectException $e) {
		    $callback = <<<EOERROR
		    parent.EDJXConfirmer.show_ok();
		    parent.NewProject.check_callback(true);
		    parent.NewProject.invalidFormat(false);
EOERROR;
		} 
	    } else {
		$callback = <<<EOERROR
		    parent.EDJXConfirmer.show_nok();
		    parent.NewProject.invalidFormat('Only a-z, A-Z, 0-9 and _ allowed. There must be at least one letter and maximum of 32 characters in length.');
EOERROR;
	    }
	    
	}
	
	
	echo <<<EOHTML
	    <!DOCTYPE html>
		<html>
		    <head><title></title></head>
		    <body>
			<script type="text/javascript">
			    {$callback}
			</script>
		    </body>
		</html>
EOHTML;
    } else {
	$gVAR['e'] = '404';
	require_once('error_handler.php');
    }
?>
